package com.train.trainmanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.train.trainmanage.model.DepartmentModel;
import com.train.trainmanage.model.LocomotiveModel;
import com.train.trainmanage.service.DepartmentService;
import com.train.trainmanage.service.LocomotiveService;

@RestController
@RequestMapping("/locomotive")
public class LocomotiveController {
	
	@Autowired
	private LocomotiveService locoService;
	@Autowired
	private DepartmentService departmentService;
	
	  @RequestMapping("/list")
		public ModelAndView userList(HttpServletRequest request, HttpServletResponse response,LocomotiveModel locomotive) {
			ModelAndView mav = new ModelAndView();
			mav.addObject("menuId", "3");
			List<LocomotiveModel> locoList = locoService.listByLoco(locomotive);
			
			
			List<DepartmentModel> departmentList = departmentService.allDepartmentList();
			
			mav.addObject("locoList", locoList);
			mav.addObject("departmentList", departmentList);
			mav.addObject("locomotive", locomotive);
			
			mav.setViewName("/locomotive/locoList");
			return mav;
		}
	    
	  
	  @RequestMapping("/getLoco")
    public @ResponseBody Map<String, Object> getUser(HttpServletRequest httpRequest, HttpServletResponse httpResponse,Integer locoId) {
 	   Map<String, Object> map = new HashMap<String, Object>();
 	   if(locoId != null ) {
 		 LocomotiveModel locomotive = locoService.getByLocoId(locoId);
 		  if(locomotive != null) {
 			 map.put("success", true);
 			 map.put("locomotive", locomotive);
 		  }else {
 			 map.put("success", true);
 			 map.put("message", "查无此车！");
 		  }
 	   }
 	   
 	   return map;
     }
	  
	  
	  
	  @RequestMapping("/save")
	    public @ResponseBody Map<String, Object> saveUser(HttpServletRequest httpRequest, HttpServletResponse httpResponse,LocomotiveModel locomotive) throws Exception {
		   Map<String, Object> map = new HashMap<String, Object>();
		   int result = 0;
		   if(locomotive != null) {
			  if(locomotive.getLocoId() != null && locomotive.getLocoId() >0) {
				  result = locoService.updateLoco(locomotive);
			  }else {
				  result = locoService.addLoco(locomotive);
			  }
		   }
			
		   map.put("success", result>0);
		   if(result>0) {
			   map.put("message", "保存成功");
		   }else {
			   map.put("message", "保存失败！！！");
		   }
		   
		   return map;
	    }

}
